// 接口配置文件
import axios from 'axios'
import { Message } from 'element-ui'

axios.defaults.baseURL = "http://localhost:3000"
axios.defaults.timeout = 120000

// 请求拦截器
axios.interceptors.request.use(request => {
    const token = sessionStorage.getItem('token')

    if(token) {
        request.headers['Authorization'] = 'Bearer ' + token;
    }
    
    return request
}, err => {
    console.log('请求拦截器异常信息', err);
})

// 返回拦截器
axios.interceptors.response.use(response => {

    if (response.data.status == false) {
        Message.error(response.data.msg)
    } 

    if (response.data.status == true) {
        Message.success(response.data.msg)
    }

    if (response.data.msg == '鉴权失败，请登录') {
        sessionStorage.clear()
        location.href = '/login'
    }

    return response.data
}, err => {
    console.log('返回拦截器异常信息', err);
})

